package com.gitee.zhziyun.cnfsystem;

import com.gitee.zhziyun.cnfsystem.ui.ChatManagerUI;
import com.gitee.zhziyun.cnfsystem.ui.LoginUI;
import com.gitee.zhziyun.cnfsystem.config.IpPortConfig;
import com.gitee.zhziyun.cnfsystem.master.AppMaster;
import com.gitee.zhziyun.cnfsystem.master.AppMasterImpl;
import com.gitee.zhziyun.cnfsystem.model.LoginData;
import com.gitee.zhziyun.cnfsystem.utils.IpAddressUtil;

public class AppGUI {
    private static AppMaster appMaster;
    private static final Object lock = new Object();

    /**
     * 初始化应用程序主控制逻辑
     * @param loginData 登录需要的数据
     */
    private static void initAppMaster(LoginData loginData) {
        IpPortConfig.setHeartBeatIp(IpAddressUtil.getBroadcastAddress(loginData.getSubnet()));            // 设置子网
        IpPortConfig.setHeartBeatPort(loginData.getHeartBeatPort());                                      // 设置心跳检测端口
        IpPortConfig.setLocalIp(IpAddressUtil.getLocalIpBySubnet(loginData.getSubnet()));                 // 设置本地IP
        appMaster = new AppMasterImpl(loginData.getUserName(), loginData.getAvatarId());                  // 创建应用程序主控制逻辑
        appMaster.start();
    }

    public static void main(String[] args) throws InterruptedException {
        LoginUI loginUI = new LoginUI(lock);        // 启动登录界面
        synchronized(lock) { lock.wait(); }         // 等待登录成功

        initAppMaster(loginUI.getLoginData());      // 初始化应用程序主控制逻辑
        new ChatManagerUI(appMaster);
    }
}
